<?php
namespace app\index\controller;
use think\Db;
use think\Request;
use think\Controller;

class Fenzu extends Base
{
    // 新增修改分组
    public function fenzuAdd()
    {
        $data = input();//token凭证 id pid name kaitou工号开头 gonghai公海工号 is_xiaoshou(0不是 1是) 

        if (empty($data['name'])) {
            apiReturn(201,'分组名称不能为空');
        }
        if ($data['pid'] == '顶级分类') {
            $data['pid'] = 0;
        }else{
            $data['pid'] = db('crm_fenzu')->where('name',$data['pid'])->value('id');
        }
        $data['date'] = time();
        if (empty($data['id'])) {
            db('crm_fenzu')->insert($data);
            apiReturn(200,'新增分组成功');
        }else{
            db('crm_fenzu')->where('id',$data['id'])->update($data);
            apiReturn(200,'修改分组成功');
        }
    }

    // 父级分组列表
    public function fenzuPidList()
    {
        $data = input();//token
        $fenzuPidList = db('crm_fenzu')->order('id ASC')->select();
        $array[] = array('id'=>0,'name'=>'顶级分类','pid'=>-1);
        foreach ($fenzuPidList as $key => $value) {
            $array[] = $value;
        }
        apiReturn(200,'获取父级产品分类列表成功',$array);
    }

    // 分组列表接口
    public function fenzuList()
    {
        $data = input();//token name is_xiaoshou

        if (isset($data['name'])) {
            if (!empty($data['name'])) {
                $where['name'] = array('like','%'.$data['name'].'%');
            }
        }
        if (isset($data['is_xiaoshou'])) {
            if (!empty($data['is_xiaoshou'])) {
                $where['is_xiaoshou'] = $data['is_xiaoshou'];
            }
        }
        if (empty($where)) {
            $where = array();
        }

        $fenzuList = db('crm_fenzu')->where($where)->select();

        apiReturn(200,'获取分组列表接口成功',$fenzuList);
    }

    // 获取分组信息接口
    public function fenzuInfo()
    {
        $data = input();//token id

        $fenzuInfo = db('crm_fenzu')->where('id',$data['id'])->find();
        if ($fenzuInfo['pid'] == 0) {
            $fenzuInfo['pid_name'] = '顶级分类';
        }else{
            $fenzuInfo['pid_name'] = db('crm_fenzu')->where('id',$fenzuInfo['pid'])->value('name');
        }
        apiReturn(200,'获取分组信息接口成功',$fenzuInfo);
    }

    // 修改分组状态
    public function fenzuStatus()
    {
        $data = input();//token id

        $status = db('crm_fenzu')->where('id',$data['id'])->value('is_xiaoshou');
        if ($status == 1) {
            $datas['is_xiaoshou'] = 0;
        }else{
            $datas['is_xiaoshou'] = 1;
        }
        db('crm_fenzu')->where('id',$data['id'])->update($datas);
        apiReturn(200,'修改成功');
    }

    // 删除分组接口
    public function fenzuDelete()
    {
        $data = input();//token id

        db('crm_fenzu')->where('id',$data['id'])->delete();
        apiReturn(200,'删除分组接口成功');
    }

}
